<template>
  <div>
    <!-- 添加按钮 -->
    <el-button type="primary" @click="willAdd">添加</el-button>

    <!-- 列表 -->
    <v-list @edit="willEdit" :list="list" @init="init"></v-list>

    <!-- 弹框 -->
    <v-form ref="form" :info="info" :list="list" @init="init"></v-form>

    <!-- 分页 -->
    <!-- 
        total：总数
        layout="prev, pager, next" prev表示上一页，next为下一页，pager表示页码列表
        page-size:设置一页的数量
    -->
    <el-pagination
      layout="prev, pager, next"
      :total="count"
      :page-size="size"
      background
      @current-change="changePage"
    ></el-pagination>
  </div>
</template>
<script>
import vList from "./components/list.vue";
import vForm from "./components/form.vue";
import { requsercount, requserlist } from "../../request/api";
import toggle from "../../mixins/toggle";
export default {
  mixins: [toggle],
  components: {
    vList,
    vForm,
  },
  mounted() {
    //请数据
    this.init();
  },
  data() {
    return {
      //列表的初始值
      list: [],
      count: 0, //管理员总数
      size: 2, //一页的数量
      page: 1, //当前的页码
    };
  },
  methods: {
    //进来获取数据
    init() {
      //请列表
      this.getList();

      //2.请总数
      requsercount().then((res) => {
        if (res.data.code == 200) {
          this.count = res.data.list[0].total;
        }
      });
    },
    getList() {
      //1.进来就请求数据
      requserlist({
        page: this.page,
        size: this.size,
      }).then((res) => {
        if (res.data.code == 200) {
          // 取回来的数据是个空数组，而且不是当前页不是第一页,取上一页的数据
          let list= res.data.list ? res.data.list : []
          if(list.length==0&&this.page!=1){
            this.page--;
            this.getList()
            return;
          }

          this.list = res.data.list ? res.data.list : [];
        }
      });
    },
    //修改了页码
    changePage(page) {
      this.page = page;
      this.getList();
    },
  },
};
</script>
<style lang="less" scoped>
</style>